package com.ptmall.app.ui.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.ui.adapter.TradePagerAdapter;
import com.ptmall.app.ui.fragment.LogFragment;
import com.ptmall.app.ui.fragment.OrderFragment;
import com.ptmall.app.ui.fragment.PtMsgFragment;
import com.ptmall.app.view.TitleBarView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class OrdersActivity extends BaseMvpActivity {

    TitleBarView titlebar;

    public TabLayout mTradeTabs;
    public ViewPager mTradeViewpager;

    private TradePagerAdapter mPagerAdapter;
    private List<Fragment> mFragmentList = new ArrayList<>();

    int current = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        current = getIntent().getIntExtra("position",0);
        setView(R.layout.activity_order);
    }

    @Override
    public void initViews() {
        titlebar = (TitleBarView) findViewById(R.id.titlebar);
        mTradeTabs = (TabLayout) findViewById(R.id.trade_tabs);
        mTradeViewpager = (ViewPager) findViewById(R.id.trade_viewpager);
        titlebar.setOnTitleBarClickListener(new TitleBarView.TitleBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });

    }

    @Override
    public void initEvents() {
        mFragmentList.add(OrderFragment.newInstance(4));
        mFragmentList.add(OrderFragment.newInstance(1));
        mFragmentList.add(OrderFragment.newInstance(2));
        mFragmentList.add(OrderFragment.newInstance(3));
        mFragmentList.add(OrderFragment.newInstance(7));
        mPagerAdapter = new TradePagerAdapter(getSupportFragmentManager(), getContext(),
                Arrays.asList(getResources().getStringArray(R.array.order_tab)),
                mFragmentList);
        mTradeViewpager.setAdapter(mPagerAdapter);
        mTradeTabs.setupWithViewPager(mTradeViewpager);
        mTradeViewpager.setOffscreenPageLimit(mFragmentList.size());//
        mTradeViewpager.setCurrentItem(current);
        mTradeTabs.setTabMode(TabLayout.MODE_SCROLLABLE);

        mTradeViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                ((OrderFragment)mFragmentList.get(position)).mRefreshView.autoRefresh();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
